﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebAppPlanProduccion.ServiceReference.AuthApi;
using Web.Framework;
using PlanProd.Contracts;
using WebAppPlanProduccion.ServiceReference.PlanProdAdmin;
using PlanProd.Contracts.Misc;
using PlanProd.Contracts.enums;
using System.ServiceModel;
using PlanProd.Contracts.Validation;
using PlanProd.Contracts.Search;

namespace WebAppPlanProduccion.Code
{
    public class WebServiceHelper
    {
        public ServiceOptions ServiceOptionsDefault
        {
            get;
            set;
        }

        public static bool Authenticate(int applicationId, int companyId, string userName, string password)
        {
            AuthApiClient client = new AuthApiClient();
            bool isAuthenticated = false;
            try
            {
                isAuthenticated = client.ValidateUser(applicationId, companyId, userName, password);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return isAuthenticated;
        }

        public static List<Company> GetCompanies(int applicationId)
        {
            AuthApiClient client = new AuthApiClient();
            List<Company> companies = new List<Company>();

            try
            {
                companies.AddRange(client.GetCompanies(applicationId));
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return companies;
        }

        public static List<string> GetOperationsForUser(int applicationId, int companyId, string userName)
        {
            AuthApiClient client = new AuthApiClient();
            List<string> operations = new List<string>();

            try
            {
                operations.AddRange(client.GetOperationsForUser(applicationId, companyId, userName));
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return operations;
        }

        public  List<Temporada> GetTemporadas(int empresaId, string nombre, DateTime? desde, DateTime? hasta, bool? activa)
        {
            List<Temporada> temporadas = new List<Temporada>();
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                temporadas.AddRange(client.GetTemporadas(this.ServiceOptionsDefault, nombre, empresaId, desde, hasta, activa));
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return temporadas;
        }      

        public  Temporada GetTemporadaById(int temporadaId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.GetTemporadaById(this.ServiceOptionsDefault, temporadaId);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public  Temporada SaveTemporada(Temporada temporada)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.SaveTemporada(this.ServiceOptionsDefault, temporada);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public  void EliminarTemporada(int temporadaId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarTemporada(this.ServiceOptionsDefault, temporadaId);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }           
        }

        public  List<CausaDesvio> GetCausasDeDesvio(int empresaId, string nombreLike)
        {
            List<CausaDesvio> desvios = new List<CausaDesvio>();
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                desvios.AddRange(client.GetCausasDesvios(this.ServiceOptionsDefault, empresaId, nombreLike));
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return desvios;
        }

        public  CausaDesvio GetCausasDeDesvioById(int causaDesvioId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.GetCausaDeDesvioById(this.ServiceOptionsDefault, causaDesvioId);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public  CausaDesvio SaveCausaDesvio(CausaDesvio causaDesvio)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.SaveCausaDesvio(this.ServiceOptionsDefault, causaDesvio);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public  void EliminarDesvioCausa(int causaDesvioId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarDesvioCausa(this.ServiceOptionsDefault, causaDesvioId);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }     

        public  List<Destino> GetDestinos(int empresaId, string nombreLike)
        {
            List<Destino> destinos = new List<Destino>();
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                destinos.AddRange(client.GetDestinos(this.ServiceOptionsDefault, empresaId, nombreLike));
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return destinos;
        }

        public  Destino GetDestinoById(int destinoId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.GetDestinoById(this.ServiceOptionsDefault, destinoId);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public  Destino SaveDestino(Destino destinoCurrent)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.SaveDestino(this.ServiceOptionsDefault, destinoCurrent);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }          
        }

        public  void EliminarDestino(int destinoId)
        {            
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarDestino(this.ServiceOptionsDefault, destinoId);
            }                       
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public  List<PedidoComercial> GetPedidosComerciales(int? pedidoId, int? temporadaId, int? destinoId, DateTime? fechaDesde, DateTime? fechaHasta, string nombreLike)
        {
            List<PedidoComercial> pedidosComerciales = new List<PedidoComercial>();
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                pedidosComerciales.AddRange(client.GetPedidosComerciales(this.ServiceOptionsDefault, pedidoId, temporadaId, destinoId, fechaDesde, fechaHasta, nombreLike));
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return pedidosComerciales;            
        }

        public  void EliminarPedidoComercial(int pedidoComercialId)
        {             
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarPedidoComercial(this.ServiceOptionsDefault, pedidoComercialId);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public  PedidoComercial SavePedidoComercial(PedidoComercial pedidoComercial)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.SavePedidoComercial(this.ServiceOptionsDefault, pedidoComercial);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public  void UpdatePedidoComercialDetalle(List<PedidoComercialDetalle> pedidoComercialDetalleList)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.UpdatePedidoComercialDetalle(this.ServiceOptionsDefault, pedidoComercialDetalleList);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public  PedidoComercial GetPedidoComercialById(int id)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.GetPedidoComercialById(this.ServiceOptionsDefault, id);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public  void EliminarPedidoComercialDetalle(int pedidoComercialDetalleId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarPedidoComercialDetalle(this.ServiceOptionsDefault, pedidoComercialDetalleId);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public  void EliminarPedidoComercialDetalleAll(int pedidoComercialId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarPedidoComercialDetalleAll(this.ServiceOptionsDefault, pedidoComercialId);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }     

        public List<ProductoValidation> ValidarProductos(List<ProductoValidation> productosAValidar)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.ValidarProductos(productosAValidar);
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public List<Proveedor> GetProveedores(ServiceOptions serviceOptions, SearchProveedorCriteria criteria)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.GetProveedores(serviceOptions, criteria);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public  List<PresupuestoProduccion> GetPresupuestosDeProduccion(ServiceOptions searchOptions, int? presupuestoId, int? temporadaId, int? destinoId, int? proveedorId, EstadoAprobado? estado, DateTime? fechaDesde, DateTime? fechaHasta)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.GetPresupuestoProduccion(searchOptions, presupuestoId, temporadaId, destinoId, proveedorId, estado, fechaDesde, fechaHasta);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }


        public  void EliminarPresupuestoProduccion(int presupuestoProduccionId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarPresupuestoProduccion(this.ServiceOptionsDefault, presupuestoProduccionId);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public  void AprobarPresupuestoProduccion(int presupuestoProduccionId, int companyId, string userName, bool aprobado)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.AprobarPresupuestoProduccion(this.ServiceOptionsDefault, presupuestoProduccionId, companyId, userName, aprobado);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public  void UpdatePresupuestoProduccionDetalle(List<PresupuestoProduccionDetalleProd> detalles)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.UpdatePresupuestoProduccionDetalle(this.ServiceOptionsDefault, detalles);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public  PresupuestoProduccion SavePresupuestoProduccion(PresupuestoProduccion presupuestoProduccion)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.SavePresupuestoProduccion(this.ServiceOptionsDefault, presupuestoProduccion);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public  PresupuestoProduccion GetPresupuestoProduccionById(int id)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.GetPresupuestoProduccionById(this.ServiceOptionsDefault, id);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }       
        }

        public List<Producto> GetProductosForPresupuesto(string codigoProducto, string descripcionProducto, int presupuestoProduccionId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.GetProductosForPresupuesto(this.ServiceOptionsDefault, codigoProducto, descripcionProducto, presupuestoProduccionId);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }       
        }

        public  void UpdatePresupuestoProduccionItems(int presupuestoProduccionId, List<UpdateCantidadPlanProd> updates, List<int> deletedProductItems)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {               
                client.UpdatePresupuestoProduccionItems(this.ServiceOptionsDefault, presupuestoProduccionId, updates, deletedProductItems);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }       
        }

        public  List<PlanProduccion> GetPlanProduccion(ServiceOptions searchOptions, int? planProduccionId, int? temporadaId, int? destinoId, int? proveedorId, DateTime? fechaDesde, DateTime? fechaHasta)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.GetPlanProduccion(searchOptions, planProduccionId, temporadaId, destinoId, proveedorId, fechaDesde, fechaHasta);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }       
        }

        public  void EliminarPlanProduccion(int planProduccionId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarPlanProduccion(this.ServiceOptionsDefault, planProduccionId);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }       
        }

        public  void UpdatePlanProduccionDetalle(List<PlanProduccionDetalleProd> planProduccionDetalles)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.UpdatePlanProduccionDetalle(this.ServiceOptionsDefault, planProduccionDetalles);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }       
        }

        public  PlanProduccion SavePlanProduccion(PlanProduccion planProduccion)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.SavePlanProduccion(this.ServiceOptionsDefault, planProduccion);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }       
        }

        public  PlanProduccion GetPlanProduccionById(int id, List<int> detalleIds)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.GetPlanProduccionById(this.ServiceOptionsDefault, id, detalleIds);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }       
        }

        public PlanProduccion GetPlanProduccionById(int id)
        {
           return GetPlanProduccionById(id, null);
        }

        public  List<Producto> GetPossibleProductosForPlan(string filter, int planProduccionId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.GetPossibleProductosForPlan(this.ServiceOptionsDefault, filter, planProduccionId);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }       
        }

        public  List<Producto> GetProductosForPlan(string filter, int planProduccionId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.GetProductosForPlan(this.ServiceOptionsDefault, filter, planProduccionId);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<Producto> SearchProducts(ServiceOptions serviceOptions, SearchProductCriteria criteria)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.SearchProducts(serviceOptions, criteria);
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public  void UpdatePlanProduccionItems(int planProduccionId, List<UpdateCantidadPlanProd> updates, List<int> deletedProductItems)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.UpdatePlanProduccionItems(this.ServiceOptionsDefault, planProduccionId, updates, deletedProductItems);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }       
        }

        public  Desvio GetDesvioById(int desvioId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.GetDesvioById(this.ServiceOptionsDefault, desvioId);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }       
        }

        public  List<Desvio> GetDesvios(ServiceOptions searchOptions, int? id, int? planProduccionId, string producto, string color, TipoDesvio? tipoDesvio, EstadoCharAprobado? estadoAprobado, int? causaDesvio, int? destinoId, int? temporadaId, int? proveedorId, DateTime? fechaDesde, DateTime? fechaHasta)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.GetDesvios(searchOptions, id, planProduccionId, producto, color, tipoDesvio, estadoAprobado, causaDesvio, destinoId, temporadaId, proveedorId, fechaDesde, fechaHasta);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }       
        }

        public  Desvio GetDesvioByProductoDelPlanDeProduccion(int detalleProductoId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.GetDesvioByProductoDelPlanDeProduccion(this.ServiceOptionsDefault, detalleProductoId);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }       
        }

        public  Desvio SaveDesvio(Desvio desvio)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.SaveDesvio(this.ServiceOptionsDefault, desvio);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }       
        }

        public  List<Desvio> SaveDesvioMasivo(List<Desvio> desvios)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.SaveDesvioMasivo(this.ServiceOptionsDefault, desvios);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public  List<Desvio> SaveYAprobarDesvioMasivo(List<Desvio> desvios, string userName, EstadoCharAprobado aprobado)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                return client.SaveYAprobarDesvioMasivo(this.ServiceOptionsDefault, desvios, userName, aprobado);
            }
            catch (Exception ex)
            {

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public  void EliminarDesvio(int desvioId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarDesvio(this.ServiceOptionsDefault, desvioId);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public  void AprobarDesvio(int desvioId, string userName, EstadoCharAprobado aprobado)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.AprobarDesvio(this.ServiceOptionsDefault, desvioId, userName, aprobado);
            }
            catch (Exception ex)
            {
                

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }       
        }
    }
}